<?php
declare(strict_types=1);
namespace app\common\service\ReportService;

use app\model\BbcModel\BusinessMember;
use app\model\BbcModel\Order;
use app\model\BbcModel\StoreMember;
use app\Normal;

class MemberLogService
{

    public function memberLogList($param){
        //获得当点12个月的年月日
        $yuef = [];
        $currentTime = time();
        if(!empty($param['year'])){
            $currentTime=strtotime($param['year'].'-01-01');
        }
        $cyear = floor(date("Y",$currentTime));
        $cMonth = floor(date("1",$currentTime));
        for($i=0;$i<12;$i++){
            $nMonth = $cMonth+$i;
            $cyear = $nMonth == 0 ? ($cyear-1) : $cyear;//年
            $nMonth = $nMonth <= 0 ? 12+$nMonth : $nMonth;//月
            $date = $cyear."-".$nMonth."-1";
            $firstday = date('Y-m', strtotime($date));//当月第一天(年月)
            $yuef[]=$firstday;
        }
        //店铺
        $where=[
            ['cid','=',$param['cid']],
            '_groupBy_'=>'loginTime',
            '_fields_'=>"FROM_UNIXTIME(cd,'%Y-%m') as loginTime,count(*) as num",
        ];
        $store= StoreMember::instance()->lists($where);
        if(!empty($store)){
            $store=array_column($store,'num','loginTime');
        }
        //小B
        $where=[
            ['cid','=',$param['cid']],
            '_groupBy_'=>'loginTime',
            '_fields_'=>"FROM_UNIXTIME(cd,'%Y-%m') as loginTime,count(*) as num",
        ];
        $channel= BusinessMember::instance()->lists($where);
        if(!empty($channel)){
            $channel=array_column($channel,'num','loginTime');
        }
        $data=[];
        foreach ($yuef as $v){
            $item['date']=$v;
            $storeMember=0;
            $channelMember=0;
            if(isset($store[$v])){
                $storeMember=$store[$v];
            }
            if(isset($channel[$v])){
                $channelMember=$channel[$v];
            }
            $item['number']=$storeMember+$channelMember;
            $data[]=$item;
        }
        return Normal::returnCode(Normal::SUC,'请求成功',$data);
    }

    /**
     * 用户下单率
     */
    public function memberOrderRatio($param)
    {
        if(empty($param['cid'])){
            return Normal::returnCode(Normal::FAIL,'except param cid');
        }
        if(!empty($param['start_time'])){
            $startTime=strtotime($param['start_time']);
        }else{
            $today=strtotime(date('Y-m-d',time()));
            $startTime=$today-86400*7;
        }
        if(!empty($param['end_time'])){
            $endTime=strtotime($param['end_time']);
        }else{
            $endTime=$startTime+86400*7+86399;
        }
        $day=7;
        if(!empty($param['day'])){
            $day=$param['day'];
        }
        $timeLimit=$day*86400;
        $registerNumber=0;
        $orderNumber=0;
        //店铺注册用户
        $where=[
            ['cid','=',$param['cid']],
            ['cd','>=',$startTime],
            ['cd','<=',$endTime],
            '_fields_'=>"mid,mn_sid,cd",
        ];
        $store= StoreMember::instance()->lists($where);
        $registerNumber+=count($store);
        foreach ($store as $v){
            $orderTime=$v['cd']+$timeLimit;
            $orderWhere=[
                ['buy_mid','=',$v['mid']],
                ['mn_sid','=',$v['mn_sid']],
                ['mnid','=',4],
                ['status','>',10],
                ['cd','<',$orderTime],
            ];
            $order= Order::instance()->countNum($orderWhere);
            if($order>0){
                $orderNumber+=1;
            }
        }
        //小B注册用户
        $channelWhere=[
            ['cid','=',$param['cid']],
            ['cd','>=',$startTime],
            ['cd','<=',$endTime],
            '_fields_'=>"mid,cb_id,cd",
        ];
        $channel= BusinessMember::instance()->lists($channelWhere);
        $registerNumber+=count($channel);
        foreach ($channel as $v){
            $orderTime=$v['cd']+$timeLimit;
            $orderWhere=[
                ['mid','=',$v['mid']],
                ['cb_id','=',$v['cb_id']],
                ['mnid','=',2],
                ['status','>',10],
                ['cd','<',$orderTime],
            ];
            $order= Order::instance()->countNum($orderWhere);
            if($order>0){
                $orderNumber+=1;
            }
        }
        if($registerNumber>0){
            $rate=round($orderNumber/$registerNumber*100,2);
            if($rate>0){
                $rate=$rate.'%';
            }
        }else{
            $rate=0;
        }
        $data['start_time']=date('Y-m-d',$startTime);
        $data['end_time']=date('Y-m-d',$endTime);
        $data['register_number']=$registerNumber;
        $data['order_number']=$orderNumber;
        $data['rate']=$rate;
        return Normal::returnCode(Normal::SUC,'请求成功',$data);

    }

}